<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="仓库名称" prop="warehouseId">
        <el-select
          v-model="queryParams.warehouseId"
          clearable
          filterable
          placeholder="请选择仓库"
          class="!w-240px"
        >
          <el-option v-for="item in warehouse" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-select
          v-model="queryParams.productId"
          clearable
          filterable
          placeholder="请选择产品"
          class="!w-240px"
        >
          <el-option
            v-for="item in productList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <!-- <el-checkbox-group v-model="queryParams.messageType">
          <el-checkbox label="上限报警" :value="1" />
          <el-checkbox label="下限报警" :value="0" />
        </el-checkbox-group> -->
        <el-radio-group v-model="queryParams.messageType">
          <el-radio :value="1">上限报警</el-radio>
          <el-radio :value="0">下限报警</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['erp:product-message:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      @selection-change="handleSelectionChange"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
    >
      <el-table-column label="产品名称" prop="productName" min-width="260px">
        <template #default="scope">
          【{{ scope.row.brandName }}】{{ scope.row.productName }}-{{ scope.row.businessTypeName }}
        </template>
      </el-table-column>
      <el-table-column label="产品分类" align="center" prop="categoryName" min-width="100px" />
      <el-table-column label="仓库" align="center" prop="warehouseName" />
      <el-table-column label="预警上限" align="center" prop="upperLimit" />
      <el-table-column label="预警下限" align="center" prop="lowerLimit" />
      <el-table-column label="当前库存" align="center" prop="currentValue" />
      <el-table-column label="差异库存" align="center" prop="varianceValue">
        <template #default="scope">
          <div>
            <span style="color: red">{{ scope.row.varianceValue }} </span>
            <span v-if="scope.row.varianceValue">
              <el-button type="primary" link @click="handleCreateOrder(scope.row)">
                生成采购单
              </el-button>
            </span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="message" />

      <el-table-column label="操作" align="center" fixed="right" width="180">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.id)">
            详情
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['erp:product-message:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['erp:product-message:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ProductMessageForm
    :warehouse="warehouse"
    :productList="productList"
    ref="formRef"
    @success="getList"
  />
</template>

<script setup lang="ts">
import download from '@/utils/download'
import { ProductMessageApi, ProductMessageVO } from '@/api/erp/productmessage'
import ProductMessageForm from './ProductMessageForm.vue'
import { useCommonOptions } from '@/hooks/web/useOption'
/** 库存预警信息 列表 */
defineOptions({ name: 'ErpProductMessage' })
const { warehouse, productList } = useCommonOptions()
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ProductMessageVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  messageType: undefined,
  warehouseId: undefined,
  productId: undefined,
  //wain: [],
  pageNo: 1,
  pageSize: 10
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const selectionList = ref([])

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProductMessageApi.getProductMessagePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}
const handleCreateOrder = async (row: ProductMessageVO) => {
  await ProductMessageApi.createPurchaseOrder(row.id)
  message.success('生成采购单成功')
  getList()
}
const handleSelectionChange = (rows) => {
  selectionList.value = rows
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProductMessageApi.deleteProductMessage(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ProductMessageApi.exportProductMessage(queryParams)
    download.excel(data, '库存预警信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
